package com.autel;

import com.oracle.bmc.Region;
import com.oracle.bmc.auth.InstancePrincipalsAuthenticationDetailsProvider;
import com.oracle.bmc.objectstorage.ObjectStorage;
import com.oracle.bmc.objectstorage.ObjectStorageClient;
import com.oracle.bmc.objectstorage.requests.DeleteObjectRequest;
import com.oracle.bmc.objectstorage.requests.GetNamespaceRequest;
import com.oracle.bmc.objectstorage.requests.GetObjectRequest;
import com.oracle.bmc.objectstorage.requests.PutObjectRequest;
import com.oracle.bmc.objectstorage.responses.DeleteObjectResponse;
import com.oracle.bmc.objectstorage.responses.GetNamespaceResponse;
import com.oracle.bmc.objectstorage.responses.GetObjectResponse;
import com.oracle.bmc.objectstorage.responses.PutObjectResponse;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;

public class Main {
    public static void main(String[] args) {
        InstancePrincipalsAuthenticationDetailsProvider provider =
        InstancePrincipalsAuthenticationDetailsProvider.builder().build();
        ObjectStorage client = new ObjectStorageClient(provider);
        // 设置Region
        client.setRegion(Region.US_PHOENIX_1);

        System.out.println("Getting the namespace.");
        GetNamespaceResponse namespaceResponse =
                client.getNamespace(GetNamespaceRequest.builder().build());
        String namespaceName = namespaceResponse.getValue();
        System.out.println(namespaceName);
        //TODO  region, bucket
        String bucketName = "default";
        String objectName = "ob1";
        //上传文件
        PutObjectRequest putObjectRequest =
                PutObjectRequest.builder()
                        .namespaceName(namespaceName)
                        .bucketName(bucketName)
                        .objectName(objectName)
                        .putObjectBody(
                                new ByteArrayInputStream("data".getBytes(StandardCharsets.UTF_8)))
                        .build();
       PutObjectResponse putObjectResponse = client.putObject(putObjectRequest);
       System.out.println(putObjectResponse.get__httpStatusCode__());



        // 删除文件
        DeleteObjectResponse deleteResponse =
                client.deleteObject(DeleteObjectRequest.builder()
                        .namespaceName(namespaceName)
                        .bucketName(bucketName)
                        .objectName(objectName).build());
        System.out.println(deleteResponse.get__httpStatusCode__());


//        // 下载文件
//        GetObjectResponse getResponse =
//                client.getObject(
//                        GetObjectRequest.builder()
//                                .namespaceName(namespaceName)
//                                .bucketName(bucketName)
//                                .objectName(objectName)
//                                .build());


    }
}